package com.lemner.motion.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.BaseBean;
import com.lemner.motion.util.FullScreen;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ChangeNameActivity extends BaseActivity {

    private ImageView iv_back;
    private EditText et_name;
    private Button btn_done;
    private String id;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_change_name;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x0020c26f);
                iv_back=(ImageView)findViewById(R.id.iv_back);
                et_name=(EditText)findViewById(R.id.et_name);
                btn_done=(Button)findViewById(R.id.btn_done);
    }

    @Override
    public void initEvent() {
      iv_back.setOnClickListener(this);
      btn_done.setOnClickListener(this);
    }

    @Override
    public void initData() {
        id=getIntent().getStringExtra("id");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent=new Intent(ChangeNameActivity.this,ChangeUserActivity.class);
        startActivity(intent);
        finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                startActivity(new Intent(ChangeNameActivity.this,ChangeUserActivity.class));
                finish();
                break;
            case R.id.btn_done:
                if(TextUtils.isEmpty(et_name.getText().toString())){
                    showToast("请输入姓名后修改");
                }
                else
                    updateName(et_name.getText().toString());
        }

    }

    BaseListener<BaseBean> listener=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if(baseBean.getErrorCode().equals("0000"))
                forceonline(ChangeNameActivity.this);
            else if(baseBean.getReturnCode().equals("0001")) {
                showToast("修改姓名成功");
                startActivity(new Intent(ChangeNameActivity.this, ChangeUserActivity.class));
                finish();
            }
            else
                showToast("修改姓名失败");
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };


    private void updateName(String name){
        BaseModel<BaseBean> model=new BaseModel<>();
        retrofit2.Call<BaseBean>call= model.service.updatename(id,name,LoginActivity.session);
        model.callEnqueue(call,listener);
    }

}
